import { defineStore } from 'pinia';
import { UserToken } from '@/core/consts';
import { handleShowToast } from '@/core/utils';
import { queryUserInfo } from '@/core/api';
import type { UserLoginStoreType, Response, UserInfoType } from '@/core/models';
import { useUserInfoStore } from './userInfo';

export const useLoginStore = defineStore('useLoginStore', {
	state: () : UserLoginStoreType => {
		return {
			showLoginModalTogglePop: false
		};
	},
	actions: {
		/** 检查是否登录 */
		checkLogin(callBack : () => void) {
			if (!uni.getStorageSync(UserToken)) {
				this.setShowLoginModalTogglePop(true);
			} else {
				callBack();
			}
		},
		/** 获取用户数据 */
		async getUserInfo() {
			const userInfoStore = useUserInfoStore();
			const userInfo : Response<UserInfoType> = await queryUserInfo();
			if (userInfo.status) {
				userInfoStore.setUserInfo(userInfo?.data);
			} else {
				handleShowToast(userInfo.msg)
			}
		},
		/** 设置登录弹框打开或者关闭 */
		setShowLoginModalTogglePop(showLoginModalTogglePop : boolean) {
			this.showLoginModalTogglePop = showLoginModalTogglePop;
		},
	},
});